/**
 * 
 */
package hoan.nguyen.webapp.service;

import hoan.nguyen.dao.AddressDAO;
import hoan.nguyen.dao.EmployeeDAO;
import hoan.nguyen.dao.LevelDAO;
import hoan.nguyen.domain.Address;
import hoan.nguyen.domain.Employee;
import hoan.nguyen.domain.Level;
import hoan.nguyen.util.JSONUtils;

import java.util.Arrays;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * @author Horizon
 * 
 */
@Path(value="/")
public class EmployeeService {

	private EmployeeDAO employeeDAO;
	private AddressDAO addressDAO;
	private LevelDAO levelDAO;

	/**
	 * @param employeeDAO
	 *            the employeeDAO to set
	 */
	@Autowired
	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	/**
	 * @param addressDAO
	 *            the addressDAO to set
	 */
	@Autowired
	public void setAddressDAO(AddressDAO addressDAO) {
		this.addressDAO = addressDAO;
	}

	/**
	 * @param levelDAO
	 *            the levelDAO to set
	 */
	@Autowired
	public void setLevelDAO(LevelDAO levelDAO) {
		this.levelDAO = levelDAO;
	}

	/**
	 * 
	 * @return
	 */
	@GET
	@Path(value = "/getAllEmployees/{abc}")
	@Consumes(value="application/json")
	@Produces(value="application/json")
	public String getAllEmplyees(@PathParam(value="abc") String abc) {

//		List<Employee> employeeList = employeeDAO.getAll();
//		List<Address> addressList = addressDAO.getAll();
		
		Address a = new Address();
		a.setId(1);
		a.setName("abc");

//		return JSONUtils.convertObjectToJSON(employeeList);
//		return JSONUtils.convertObjectToJSON(addressList);
		return JSONUtils.convertObjectToJSON(Arrays.asList(a));
	}
	
	/**
	 * 
	 * @return
	 */
	@POST
	@Path(value = "/addAllEmployees")
	@Consumes(value="application/json")
	@Produces(value="application/json")
	public String addAllEmplyees(@RequestParam(value="abc") Address abc) {
		
//		List<Employee> employeeList = employeeDAO.getAll();
//		List<Address> addressList = addressDAO.getAll();
		
		Address a = new Address();
		a.setId(1);
		a.setName("abc");
		
//		return JSONUtils.convertObjectToJSON(employeeList);
//		return JSONUtils.convertObjectToJSON(addressList);
		return JSONUtils.convertObjectToJSON(Arrays.asList(a));
	}

	/**
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addEmployee", method = { RequestMethod.GET })
	public String loadAddEmpoyeePage(ModelMap map) {

		map.put("employee", new Employee());

		return "add-employee";
	}

	/**
	 * 
	 * @param employee
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addEmployee", method={RequestMethod.POST})
	public String addNewEmployee(@ModelAttribute Employee employee,
			@RequestParam(value = "address") String strAddress, @RequestParam(value = "level") String strLevel,
			ModelMap map) {
		
		Level level = new Level();
		level.setName(strLevel);
		level.setDes(strLevel);
		
		employee.getLevels().add(level);

		Employee addedEmployee = employeeDAO.save(employee);
		map.put("employee", addedEmployee);
		
		Employee e = employeeDAO.getById(addedEmployee.getId());
        
		return "add-employee";
	}
}
